﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.Unity;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json;
using System.Web.SessionState;
using SZHL.UI.AppCode;
using SZHL.Data;
using System.Data;

namespace SZHL.UI.API
{
    /// <summary>
    /// FTAPI 的摘要说明
    /// </summary>
    public class CommAPI : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            context.Response.ContentType = "text/plain";
            string strAction = context.Request["Action"] ?? "";
            // string strUserName = "";
            Msg_Result Model = new Msg_Result() { Action = strAction.ToUpper(), ErrorMsg = "" };
            string P1 = context.Request["P1"] ?? "";
            string P2 = context.Request["P2"] ?? "";
            if (!string.IsNullOrEmpty(strAction))
            {
                try
                {


                    //TODO: 未实现,分享页面的接口通过暂时将分享人的code传递给打开链接的人来调用接口(不安全)
                    if (strAction == "GETFOLDERBYCODE")
                    {
                        string strWhere = string.Format("sharecode='" + P1 + "' and sharepasd='" + P2 + "'").FilterSpecial();
                        string strSQLFLODER = string.Format(@"select * from FT_Folder  where 1=1 and  {0}   order by CRDate desc  ", strWhere);
                        DataTable dtFLODER = new FT_FolderB().GetDTByCommand(strSQLFLODER);

                        string strSQLFILE = string.Format(@"select  *  from FT_File  where 1=1  and  {0}   order by CRDate desc  ", strWhere);
                        DataTable dtFILE = new FT_FileB().GetDTByCommand(strSQLFILE);
                        Model.Result = dtFLODER;
                        Model.Result1 = dtFILE;
                    }
                    if (strAction == "GETSHARELIST")
                    {
                        Model.Result = new FT_FolderB().GetEntities(d => d.PFolderID.ToString() == P1);
                        Model.Result1 = new FT_FileB().GetEntities(d => d.FolderID.ToString() == P1);
                    }
                    else if (strAction == "AUTOSENDSMS") //自动发短信
                    {
                        string strSQLFILE = "select * from ZH_DXGL where isSend='N' and SendTime<getdate()";
                        DataTable dt = new ZH_DXGLB().GetDTByCommand(strSQLFILE);
                        foreach (DataRow dr in dt.Rows)
                        {
                            var tel = dr["dxnums"];
                            var con = dr["dxContent"];
                            if (tel != null && tel.ToString() != "" && con != null && con.ToString() != "")
                            {
                                CommonHelp.SendSMS(tel.ToString(),con.ToString());
                                string strSQLFILE1 = string.Format(@"update ZH_DXGL set isSend='Y' where ID='{0}'", dr["ID"].ToString());
                                new ZH_DXGLB().ExsSql(strSQLFILE1);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Model.ErrorMsg = "操作异常，请重试";
                    Model.Result = ex.Message;
                }

            }
            IsoDateTimeConverter timeConverter = new IsoDateTimeConverter();
            timeConverter.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            string Result = JsonConvert.SerializeObject(Model, Formatting.Indented, timeConverter).Replace("null", "\"\"");
            context.Response.Write(Result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}